﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Library;

namespace Sport_Shop.Winform
{
    public partial class QLSanPhamUpdate : Form
    {
        public QLSanPhamUpdate()
        {
            InitializeComponent();
        }

        //Truyền DataSource mới cho GridViewSanPham
        public delegate void SetSPMoi(SanPham sp);
        public SetSPMoi SetSP;

        //Đặt flag để xác định new hoặc update
        public string flag { get; set; }

        //Lấy Current SanPham
        public SanPham CurrentSP { get; set; }

        //Browse hình
        #region Browse hình
        private void btBrowse_Click(object sender, EventArgs e)
        {
            string currentDir = Directory.GetCurrentDirectory();
            string path = currentDir.Remove(currentDir.Length - 29);

            OpenFileDialog Hinh = new OpenFileDialog();
            Hinh.Title = "Chọn hình";
            Hinh.Filter = "JPG Files|*.jpg|PNG Files|*.png";
            Hinh.InitialDirectory = @path + "\\Sport_Shop\\Image\\SanPham";
            if (Hinh.ShowDialog() == DialogResult.OK)
            {
                tbHinh.Text = Hinh.FileName.Substring(Hinh.FileName.LastIndexOf("\\") + 1);
            }
        }
        #endregion

        //Form Load
        #region Form Load
        private void QLSanPhamUpdate_Load(object sender, EventArgs e)
        {
            label9.Visible = false;

            if (flag == "new")
            {
                btOK.Text = "Thêm";

                tbMaSP.Clear();
                tbGia.Clear();
                tbHinh.Clear();
                tbHSX.Clear();
                tbMau.Clear();
                tbSize.Clear();
                tbTenSP.Clear();

                List<LoaiSanPham> lsp = LoaiSanPham.GetList();
                cbLoaiSP.DisplayMember = "TenLoai";
                cbLoaiSP.ValueMember = "MaLoai";
                cbLoaiSP.DataSource = lsp;

                List<MonTheThao> mtt = MonTheThao.GetList();
                cbMTT.DisplayMember = "TenMTT";
                cbMTT.ValueMember = "MaMTT";
                cbMTT.DataSource = mtt;
            }

            else if (flag == "update")
            {
                btOK.Text = "Sửa";

                List<LoaiSanPham> lsp = LoaiSanPham.GetList();
                cbLoaiSP.DisplayMember = "TenLoai";
                cbLoaiSP.ValueMember = "MaLoai";
                cbLoaiSP.DataSource = lsp;

                List<MonTheThao> mtt = MonTheThao.GetList();
                cbMTT.DisplayMember = "TenMTT";
                cbMTT.ValueMember = "MaMTT";
                cbMTT.DataSource = mtt;

                tbMaSP.Text = CurrentSP.MaSP;
                tbTenSP.Text = CurrentSP.TenSP;
                tbHSX.Text = CurrentSP.TenHSX;
                tbGia.Text = CurrentSP.DonGia.ToString();
                tbHinh.Text = CurrentSP.HinhAnh;
                tbMau.Text = CurrentSP.MauSac;
                tbSize.Text = CurrentSP.Size;
                cbLoaiSP.SelectedValue = CurrentSP.MaLoai;
                cbMTT.SelectedValue = CurrentSP.MaMTT;
            }
        }
        #endregion

        //Button OK
        #region Click OK
        private void btOK_Click(object sender, EventArgs e)
        {
            if (tbTenSP.Text.Trim() != "" && tbHSX.Text.Trim() != "" && tbGia.Text.Trim() != "" && tbHinh.Text.Trim() != "" && tbMau.Text.Trim() != "" && tbSize.Text.Trim() != "")
            {
                if (flag == "new")
                {
                    SanPham sp = new SanPham(-1, tbMaSP.Text.Trim(),
                        tbTenSP.Text.Trim(),
                        tbHSX.Text.Trim(),
                        tbGia.Text.Trim(),
                        tbHinh.Text.Trim(),
                        tbMau.Text.Trim(),
                        tbSize.Text.Trim(),
                        (LoaiSanPham)(cbLoaiSP.SelectedItem),
                        (MonTheThao)(cbMTT.SelectedItem));

                    SetSP(sp);

                    this.Dispose();
                }

                else if (flag == "update")
                {
                    SanPham sp = new SanPham(CurrentSP.ID, tbMaSP.Text.Trim(),
                        tbTenSP.Text.Trim(),
                        tbHSX.Text.Trim(),
                        tbGia.Text.Trim(),
                        tbHinh.Text.Trim(),
                        tbMau.Text.Trim(),
                        tbSize.Text.Trim(),
                        (LoaiSanPham)(cbLoaiSP.SelectedItem),
                        (MonTheThao)(cbMTT.SelectedItem));

                    sp.updated = true;

                    SetSP(sp);

                    this.Dispose();
                }
            }
            else
            {
                label9.Text = "Vui lòng điền đầy đủ thông tin !!!!";
                label9.ForeColor = Color.Red;
                label9.Visible = true;
            }
        }
        #endregion
    }
}
